﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZyGames.DZPocker.Model;
using ZyGames.DZPocker.Script.CsScript.Action;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;

namespace ZyGames.DZPocker.Bll.Logic
{
	/// <summary>
	/// 游戏房间(单例)
	/// </summary>
	public class GameRoom
	{
		private static GameRoom _instance;
		static GameRoom()
		{
			_instance = new GameRoom();
			var roomStruct = new MemoryCacheStruct<RoomData>();
			var roomList = new ShareCacheStruct<RoomInfo>().FindAll(false);
			foreach (var roomInfo in roomList)
			{
				var roomData = new RoomData() { RoomId = roomInfo.Id };
				string key = roomData.RoomId.ToString();
				roomStruct.TryAdd(key, roomData);
			}
		}

		/// <summary>
		/// 房间
		/// </summary>
		private GameRoom()
		{
			_roomStruct = new MemoryCacheStruct<RoomData>();
			_roomConfig = new ShareCacheStruct<RoomInfo>();
		}

		public static GameRoom Current
		{
			get { return _instance; }
		}

		private MemoryCacheStruct<RoomData> _roomStruct;
		/// <summary>
		/// 数据库中几倍积分房相关的配置
		/// </summary>
		private ShareCacheStruct<RoomInfo> _roomConfig;

		/// <summary>
		/// 房间列表
		/// </summary>
		public List<RoomInfo> RoomList
		{
			get { return _roomConfig.FindAll(); }
		}

		/// <summary>
		/// 根据
		/// </summary>
		/// <param name="roomId"></param>
		/// <returns></returns>
		public RoomInfo GetRoom(int roomId)
		{
			return _roomConfig.FindKey(roomId);
		}

		public string Tip(string format, params object[] args)
		{
			return string.Format(format, args);
		}

		/// <summary>
		/// 进入房间
		/// </summary>
		/// <param name="user"></param>
		/// <param name="roomInfo"></param>
		public void EnterRoomAndCreateTable(GameUser user, RoomInfo roomInfo, out TableData table)
		{
			table = null;
			string key = roomInfo.Id.ToString();
			RoomData roomData;
			if (_roomStruct.TryGet(key, out roomData))
			{
				var cacheSet = new GameDataCacheSet<GameUser>();
				var userTemp = cacheSet.FindKey(user.PersonalId, user.GetUserId());
				var list = cacheSet.FindGlobal(t => true);

				var tableData = GameTable.Current.CreateTable(userTemp, roomData, roomInfo);
				if (tableData != null && tableData.Positions.Length > 0)
				{
					table = tableData;
					//通知进入房间 TODO：通知报错，还需要排查
					//GameTable.Current.SyncNotifyAction((int)ActionType.DZEnterRoom, tableData, null,
					//c =>
					//{
					//	GameTable.Current.CheckStart(tableData);
					//});
				}
			}
		}

		/// <summary>
		/// 加入指定房间
		/// </summary>
		/// <param name="user"></param>
		/// <param name="roomInfo"></param>
		/// <param name="table"></param>
		/// <returns></returns>
		public bool JoinTable(GameUser user, RoomInfo roomInfo, TableData table)
		{
			string key = roomInfo.Id.ToString();
			RoomData roomData;
			if (_roomStruct.TryGet(key, out roomData))
			{
				//TODO:消息广播
				//var cacheSet = new GameDataCacheSet<GameUser>();
				//var userTemp = cacheSet.FindKey(user.PersonalId, user.UserId);
				//var list = cacheSet.FindGlobal(t => true);
				return GameTable.Current.JoinTable(user, table, roomData);
			}
			return false;
		}

		/// <summary>
		/// 离开房间
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public bool Exit(GameUser user, TableData table)
		{
			GameTable.Current.ExitTablePosition(user, table);
			return true;
		}

		/// <summary>
		/// 获取房间数据
		/// </summary>
		/// <param name="roomId"></param>
		/// <returns></returns>
		public RoomData GetRoomData(int roomId)
		{
			string key = roomId.ToString();
			RoomData roomData;
			if (_roomStruct.TryGet(key, out roomData))
			{
				return roomData;
			}
			return null;
		}

		/// <summary>
		/// TODO:直接根据GameUser获得Table跟目前项目需求一个玩家可以参与多个牌局不相符合，需要修改
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		//public TableData GetTableData(GameUser user)
		//{
		//	var tableData = GetTableData(user.Property.TableId, user.Property.RoomId);
		//	if (tableData == null)
		//	{
		//		Tool.WriteWarmingLine(string.Format("GetTableData error,uid:{0},Room:{1},TableId:{2}", user.GetUserId(), user.Property.RoomId, user.Property.TableId));
		//	}
		//	//if (tableData == null)
		//	//{
		//	//    var param = new Parameters();
		//	//    param.Add("FleeUserId", 0);
		//	//    var list = new List<GameUser>();
		//	//    list.Add(user);
		//	//    GameTable.Current.SyncNotifyAction(ActionIDDefine.Cst_Action2013, list, param, null);
		//	//}
		//	return tableData;
		//}

		public TableData GetTableData(int tableId, int roomId = 1001)
		{
			RoomData roomData = GetRoomData(roomId);
			if (roomData != null)
			{
				TableData table;
				if (roomData.Tables.TryGetValue(tableId, out table))
				{
					return table;
				}
			}
			return null;
		}
	}
}
